package lg.minecraft.plugin.cobblemoncore;

import com.cobblemon.mod.common.Cobblemon;
import com.cobblemon.mod.common.util.IdentifierExtensionsKt;
import lg.minecraft.plugin.cobblemoncore.command.MainCommand;
import lg.minecraft.plugin.cobblemoncore.config.MainConfig;
import lg.minecraft.plugin.cobblemoncore.config.MessageConfig;
import lg.minecraft.plugin.cobblemoncore.event.ConfigReloadEvent;
import lg.minecraft.plugin.cobblemoncore.util.CobblemonEventHandler;
import lg.minecraft.plugin.cobblemoncore.util.PokemonDataCache;
import lg.minecraft.plugin.cobblemoncore.util.PokemonUtil;
import lg.minecraft.plugin.library.LGPlugin;
import lombok.Getter;
import net.minecraft.class_2960;
import org.bukkit.Bukkit;

import java.io.File;

public final class LGCobblemonCore extends LGPlugin {

    @Getter private static LGCobblemonCore instance;
    @Getter private static File mappingDirectory;

    @Getter private static MainConfig mainConfig;
    @Getter private static MessageConfig messageConfig;

    @Override
    public void onEnable() {
        if (checkCobblemonDependency()) return;
        instance = this;
        mappingDirectory = new File(getDataFolder(), "mapping");
        mainConfig = new MainConfig();
        messageConfig = new MessageConfig();
        MainCommand mainCommand = new MainCommand();
        this.loadConfigData();
    }

    @Override
    public void onDisable() {
        CobblemonEventHandler.unregisterAllCobblemonEvent();
    }

    public void loadConfigData() {
        mainConfig.load();
        messageConfig.load();

        PokemonUtil.yes = messageConfig.getYes();
        PokemonUtil.no = messageConfig.getNo();
        PokemonUtil.none = messageConfig.getNone();

        PokemonUtil.NatureTypeFormat = messageConfig.getNatureTypeFormat();
        PokemonUtil.ATTACK_UP = messageConfig.getAttackUp();
        PokemonUtil.SPECIAL_ATTACK_UP = messageConfig.getSpecialAttackUp();
        PokemonUtil.SPECIAL_DEFENSE_UP = messageConfig.getSpecialDefenseUp();
        PokemonUtil.DEFENSE_UP = messageConfig.getDefenseUp();
        PokemonUtil.SPEED_UP = messageConfig.getSpeedUp();
        PokemonUtil.ATTACK_DOWN = messageConfig.getAttackDown();
        PokemonUtil.SPECIAL_ATTACK_DOWN = messageConfig.getSpecialAttackDown();
        PokemonUtil.SPECIAL_DEFENSE_DOWN = messageConfig.getSpecialDefenseDown();
        PokemonUtil.DEFENSE_DOWN = messageConfig.getDefenseDown();
        PokemonUtil.SPEED_DOWN = messageConfig.getSpeedDown();
        PokemonUtil.BALANCED = messageConfig.getBalanced();

        if (!mappingDirectory.exists()) {
            if (!mappingDirectory.mkdirs()) {
                getLogger().severe("Failed to create mapping directory!");
            }
        }

        PokemonDataCache.getInstance().loadData();

        Bukkit.getPluginManager().callEvent(new ConfigReloadEvent());
    }

    private boolean checkCobblemonDependency() {
        try {
            Cobblemon.INSTANCE.getImplementation();
        } catch (Exception e) {
            getLogger().severe("未找到Cobblemon模组！插件将被禁用！");
            getServer().getPluginManager().disablePlugin(this);
            return true;
        }
        return false;
    }

    public static String simplify(class_2960 name) {
        return IdentifierExtensionsKt.simplify(name, "cobblemon");
    }

    public static class_2960 createCobblemonResource(String name) {
        return class_2960.method_60655("cobblemon", name);
    }

}
